<?php

namespace app\admin\model\user;

use app\admin\model\course\Order;
use fast\Random;
use think\Model;
use think\Validate;


class Student extends Model
{

    

    

    // 表名
    protected $name = 'user_student';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    public function genderList(){
        return [
            0 => "女",
            1 => "男",
        ];
    }







    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getList($condition = []){
        $list = $this->where($condition)->order("id","desc")->select();
        return $list;
    }

    public function addStudent($param){
        $validate = new Validate([
            ["name","require","学员姓名必填"],
            ["gender","require","性别必填"],
            ["birthday","require","出生日期必填"],
//            ["school_name","require","学校必填"],
        ]);
        $result = $validate->check($param);
        if(!$result){
            exception((string)$validate->getError());
        }

        $this->save($param);
    }


    public function editStudent($param){
        $validate = new Validate([
            ["id","require","请选择学员"],
            ["name","require","学员姓名必填"],
            ["gender","require","性别必填"],
            ["birthday","require","出生日期必填"],
//            ["school_name","require","学校必填"],
        ]);
        $result = $validate->check($param);
        if(!$result){
            exception((string)$validate->getError());
        }
        $student = $this->where("user_id",$param['user_id'])
            ->where("id",$param['id'])
            ->find();
        if(!$student){
            exception("未找到该学员");
        }

        $student->save($param);
    }

    /*
     * 删除学员
     * */
    public function delStudent(){
        //检测当前学员下面是否有正在进行的课程
        $student_order = Order::where("student_id",$this['id'])->where("status","in",Order::getInStatusArr())->find();
        if($student_order){
            exception($this['name']."存在正在进行的课程，无法删除");
        }
        $this->delete();
        return true;
    }

    public function getRemarkAttr($value){
        if(is_null($value)){
            return "";
        }
        return  $value;
    }
}
